import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["page", "size"];

/**
 * @date: 2018-07-27

 */
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { getResponse } from 'utils/utils';
import { queryMessageList as _queryMessageList, queryContent as _queryContent, queryRecipient as _queryRecipient, queryError as _queryError, resendMessage as _resendMessage, deleteMessage as _deleteMessage } from "../services/messageQueryService";
export default {
  namespace: 'messageQuery',
  state: {
    statusList: [],
    // 状态列表
    messageTypeList: [],
    // 消息类型
    messageData: {},
    // 消息列表
    content: {},
    // 内容
    recipientData: {},
    // 收件人
    error: {} // 错误

  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, _getResponse, messageTypeList, statusList;

      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryMapIdpValue, {
                messageTypeList: 'CMSG.MESSAGE_TYPE',
                statusList: 'CMSG.TRANSACTION_STATUS'
              });

            case 4:
              _context.t1 = _context.sent;
              _getResponse = (0, _context.t0)(_context.t1);
              messageTypeList = _getResponse.messageTypeList;
              statusList = _getResponse.statusList;
              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  messageTypeList: messageTypeList,
                  statusList: statusList
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 获取消息数据
    queryMessageList: /*#__PURE__*/_regeneratorRuntime.mark(function queryMessageList(_ref2, _ref3) {
      var payload, call, put, _payload$page, page, _payload$size, size, params, res, list;

      return _regeneratorRuntime.wrap(function queryMessageList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _payload$page = payload.page, page = _payload$page === void 0 ? 0 : _payload$page, _payload$size = payload.size, size = _payload$size === void 0 ? 10 : _payload$size, params = _objectWithoutProperties(payload, _excluded);
              _context2.next = 5;
              return call(_queryMessageList, _objectSpread({
                page: page,
                size: size
              }, params));

            case 5:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  messageData: list
                }
              });

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, queryMessageList);
    }),
    // 获取内容信息
    queryContent: /*#__PURE__*/_regeneratorRuntime.mark(function queryContent(_ref4, _ref5) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function queryContent$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.next = 4;
              return call(_queryContent, payload);

            case 4:
              res = _context3.sent;
              list = getResponse(res);

              if (!list) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  content: list
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, queryContent);
    }),
    // 获取收件人信息
    queryRecipient: /*#__PURE__*/_regeneratorRuntime.mark(function queryRecipient(_ref6, _ref7) {
      var payload, call, put, messageId, tenantId, _payload$page2, page, _payload$size2, size, res, list;

      return _regeneratorRuntime.wrap(function queryRecipient$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call, put = _ref7.put;
              messageId = payload.messageId, tenantId = payload.tenantId, _payload$page2 = payload.page, page = _payload$page2 === void 0 ? 0 : _payload$page2, _payload$size2 = payload.size, size = _payload$size2 === void 0 ? 10 : _payload$size2;
              _context4.next = 5;
              return call(_queryRecipient, messageId, {
                tenantId: tenantId,
                page: page,
                size: size
              });

            case 5:
              res = _context4.sent;
              list = getResponse(res);

              if (!list) {
                _context4.next = 10;
                break;
              }

              _context4.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  recipientData: list
                }
              });

            case 10:
            case "end":
              return _context4.stop();
          }
        }
      }, queryRecipient);
    }),
    // 获取错误信息
    queryError: /*#__PURE__*/_regeneratorRuntime.mark(function queryError(_ref8, _ref9) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function queryError$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call, put = _ref9.put;
              _context5.next = 4;
              return call(_queryError, payload);

            case 4:
              res = _context5.sent;
              list = getResponse(res);

              if (!list) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  error: list
                }
              });

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, queryError);
    }),
    // 重试
    resendMessage: /*#__PURE__*/_regeneratorRuntime.mark(function resendMessage(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function resendMessage$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_resendMessage, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, resendMessage);
    }),
    // 删除
    deleteMessage: /*#__PURE__*/_regeneratorRuntime.mark(function deleteMessage(_ref12, _ref13) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteMessage$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.next = 4;
              return call(_deleteMessage, payload);

            case 4:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, deleteMessage);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};